//
//  HZWuDangView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/14.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZWuDangView.h"

#define kWuDangMargin 10
#define kLineW 0.5

@interface HZWuDangView()
@property (nonatomic, strong)UIView *pVerticalLine;
@property (nonatomic, strong)NSMutableArray *pBuySellArray;
@property (nonatomic, strong)NSMutableArray *pPriceArray;
@property (nonatomic, strong)NSMutableArray *pNumArray;
@property (nonatomic, strong)NSMutableArray *pColorBgArray;
@property (nonatomic, strong)NSMutableArray *pColorArray;
@property (nonatomic, assign)CGFloat fColorW;
@property (nonatomic, assign)CGFloat fColorH;
@property (nonatomic, assign)NSInteger nMaxNum;
@property (nonatomic, strong)NSArray<HZWuDangModel *> *pModelArray;
@end

@implementation HZWuDangView
- (NSMutableArray *)pBuySellArray
{
    if (!_pBuySellArray) {
        _pBuySellArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pBuySellArray;
}
- (NSMutableArray *)pPriceArray
{
    if (!_pPriceArray) {
        _pPriceArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pPriceArray;
}
- (NSMutableArray *)pNumArray
{
    if (!_pNumArray) {
        _pNumArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pNumArray;
}
- (NSMutableArray *)pColorBgArray
{
    if (!_pColorBgArray) {
        _pColorBgArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pColorBgArray;
}
- (NSMutableArray *)pColorArray
{
    if (!_pColorArray) {
        _pColorArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pColorArray;
}

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}

- (void)layoutSubviews{
    _pVerticalLine.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2);
    _pVerticalLine.bounds = CGRectMake(0, 0, kLineW, CGRectGetHeight(self.frame));

    CGFloat fPartW = (CGRectGetWidth(self.frame) - 4*kWuDangMargin - kLineW)/2;
    CGFloat fBuySellW = 20;
    CGFloat fBgW = fPartW*2/5;
    CGFloat fItemW = (fPartW*3/5 - fBuySellW)/2;
    CGFloat fItemH = CGRectGetHeight(self.frame)/5;
    _fColorW = fBgW;
    _fColorH = 6;
    for (int i = 0; i < _pBuySellArray.count; i++) {
        UILabel *pBuySell = _pBuySellArray[i];
        UILabel *pPrice = _pPriceArray[i];
        UILabel *pNum = _pNumArray[i];
        UIView *pBg = _pColorBgArray[i];
        UIView *pColor = _pColorArray[i];
        CGFloat Y = fItemH * (i%5);//买卖宽度的的1/3给价格宽
        CGFloat buySellX = kWuDangMargin + (CGRectGetMaxX(_pVerticalLine.frame)) * (i/5);
        CGFloat priceX = buySellX + fBuySellW;
        CGFloat numX = priceX + fItemW ;
        CGFloat pointX = numX + fItemW + fBgW/2 ;
        CGFloat pointY = fItemH/2 + (fItemH * (i%5));
        pBuySell.frame = CGRectMake(buySellX, Y, fBuySellW, fItemH);
        pPrice.frame = CGRectMake(priceX, Y, fItemW, fItemH);
        pNum.frame = CGRectMake(numX, Y, fItemW, fItemH);
        pBg.center = (CGPoint){pointX, pointY};
        pBg.bounds = CGRectMake(0, 0, fBgW, _fColorH);
        pColor.frame = CGRectMake(0, 0, 0, _fColorH);
        
        if (_pModelArray) {
            HZWuDangModel *model = _pModelArray[i];
            if ([model.model_num isEqual:@"-"]) {
                continue;
            }
            NSInteger nNum = [model.model_num integerValue];
            CGFloat fColorW = (nNum * _fColorW) / _nMaxNum;
            [UIView animateWithDuration:0.25 animations:^{
            pColor.frame = CGRectMake(pColor.frame.origin.x, pColor.frame.origin.y, fColorW, _fColorH);
            }];
        }
    }
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _nMaxNum = 0;
    _pVerticalLine = [UIView new];
    _pVerticalLine.backgroundColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:nil];
    [self addSubview:_pVerticalLine];
    
    NSArray *pTitleArray = [NSArray arrayWithObjects:
                            kHZSell5,
                            kHZSell4,
                            kHZSell3,
                            kHZSell2,
                            kHZSell1,
                            kHZBuy1,
                            kHZBuy2,
                            kHZBuy3,
                            kHZBuy4,
                            kHZBuy5, nil];
    for (NSString *title in pTitleArray) {
        UILabel *pBuySell = [UILabel new];
        UILabel *pPrice = [UILabel new];
        UILabel *pNum = [UILabel new];
        UIView *pBg = [UIView new];
        UIView *pColor = [UIView new];
        pBuySell.text = title;
        pPrice.text = kDefaultContent;
        pPrice.textAlignment = NSTextAlignmentCenter;
        pPrice.adjustsFontSizeToFitWidth = YES;
        pNum.text = kDefaultContent;
        pNum.textAlignment = NSTextAlignmentRight;
        pNum.adjustsFontSizeToFitWidth = YES;
        pBg.backgroundColor = kMYHexToRGB(0xe8e8e8, 0.5);
        pNum.textColor = kMYHexToRGB(0x858585, 1);
        pBuySell.font = pPrice.font = pNum.font = [UIFont systemFontOfSize:11];
        [self addSubview:pBuySell];
        [self addSubview:pPrice];
        [self addSubview:pNum];
        [self addSubview:pBg];
        [pBg addSubview:pColor];
        [self.pBuySellArray addObject:pBuySell];
        [self.pPriceArray addObject:pPrice];
        [self.pNumArray addObject:pNum];
        [self.pColorBgArray addObject:pBg];
        [self.pColorArray addObject:pColor];
        
        pBg.layer.cornerRadius = 3;
        pBg.layer.masksToBounds = YES;
    }
}

#pragma mark -
#pragma mark public
- (void)updateNewData:(HZMinPriceModel *)data
{
    NSArray *pTempArray = data.model_wuDangArray;
    if (!pTempArray || pTempArray.count == 0) {
        return;
    }
    _nMaxNum = 0;
    _pModelArray = pTempArray;
    
    for (HZWuDangModel *model in pTempArray) {
        NSInteger nNum = [model.model_num integerValue];
        _nMaxNum = MAX(_nMaxNum, nNum);
    }
    if (_nMaxNum == 0) {
        return;
    }
    for (int i = 0; i < pTempArray.count; i++) {
        HZWuDangModel *wudangModel = pTempArray[i];
        wudangModel.model_prePrice = data.model_prePrice;
        UILabel *priceLabel = _pPriceArray[i];
        UILabel *numLabel = _pNumArray[i];
        UIView  *colorView = _pColorArray[i];
        priceLabel.text = wudangModel.model_price;
        numLabel.text = wudangModel.model_num;
        
        colorView.backgroundColor = kMYHexToRGB(0x30ae0e,1);
        if (i > 4) {
            colorView.backgroundColor = kMYHexToRGB(0xf60300,1);
        }
        if ([wudangModel.model_price floatValue] > [wudangModel.model_prePrice floatValue]) {
            priceLabel.textColor = kMYHexToRGB(0xf60300,1);
        }else if ([wudangModel.model_price floatValue] < [wudangModel.model_prePrice floatValue]) {
            priceLabel.textColor = kMYHexToRGB(0x30ae0e,1);
        }else {
            priceLabel.textColor = kMYHexToRGB(0x858585,1);
        }
    }
    [self setNeedsLayout];
}

@end
